package com.wisdytech.linkdcs.ieMaintenance.action;


import com.wisdytech.common.handler.ResourceMatchingService;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipStop.model.EquipStop;
import com.wisdytech.linkdcs.equipStop.service.IEquipStopService;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.ieMaintenance.model.IeMaintenance;
import com.wisdytech.linkdcs.ieMaintenance.model.TmMaterial;
import com.wisdytech.linkdcs.ieMaintenance.service.IIeMaintenanceService;
import com.wisdytech.linkdcs.system.model.SysUser;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenbin
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/equipment/ieMaintenance")
public class IeMaintenanceAction {

	
    /**
     * 注入iEquipStopService
     */
    @Autowired
    @Qualifier("iIeMaintenanceService")
    private IIeMaintenanceService iIeMaintenanceService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
    /**
    *
    * <p>Description: 设备信息</p>
    * @param model model
    * @return String
    */
   @RequestMapping("/index")
   public String equipmentIndex(Model model, HttpServletRequest httpRequest){
       this.resourceMatchingService.getResourceName(httpRequest,model);
       return "ieMaintenance/ieIndex";
   }
   
   /**
    * 
    * <p>Description: 根据产品编号模糊匹配获取数据</p>
    * @param equipCode
    * @return
    */
   @RequestMapping("/searchProductByProductCode/get.mvc")
   @ResponseBody
   public StandardResult searchProductByProductCode(Filter filter ,@ModelAttribute("productCode")String productCode){
       PageInfo<TmMaterial>  pageInfo =
    		   iIeMaintenanceService.searchProductByProductCode(filter, productCode);
       //判断是否是最后一页
       if(filter.getNum()>=pageInfo.getPages()){
           return StandardResult.getFailResult("",pageInfo.getList());
       }else{
           return StandardResult.getSuccessResult(pageInfo.getList());
       }
   }
   
   /**
    * 
    * <p>Description: 获取单位</p>
    * @return
    */
   @RequestMapping("/getProductUnitbyPkId/get.mvc")
   @ResponseBody
   public StandardResult getProductUnitbyPkId(String pkId){
	   TmMaterial TmMaterial = iIeMaintenanceService.getProductUnitbyPkId(pkId);
	   return StandardResult.getSuccessResult(TmMaterial);
   }
   
   
   /**
    * @param filter 分页参数
    * @throws IOException 
    */
   @RequestMapping(value = "/listIeMaintenanceByFilter/get.mvc")
   @ResponseBody
   public StandardTableResult<IeMaintenance> listStopByFilter(String equipCode,String productCode,Filter filter) throws IOException{
   		PageInfo<IeMaintenance> ieMaintenances =this.iIeMaintenanceService.listIeMaintenanceByFilter(equipCode,productCode,filter);
       return new StandardTableResult<IeMaintenance>(ieMaintenances.getTotal(),ieMaintenances.getList());
   }
   
   /**
    * 保存
    * @param property 属性对象
    * @param session 
    * @return 保存结果集
    */
   @RequestMapping(value = "/saveIEMaintenance/save.mvc")
   @ResponseBody
   public StandardResult saveIEMaintenance(IeMaintenance ieMaintenance, HttpSession session) {
       SysUser sysUser = BaseUtil.getCurrentUser();
       String loginName = sysUser.getUsername();
       if (StringUtils.isBlank(ieMaintenance.getPkId())) {
    	   iIeMaintenanceService.addIEMaintenance(ieMaintenance, loginName);
       }else {
    	   iIeMaintenanceService.updateIEMaintenance(ieMaintenance, loginName);
       }
       return  StandardResult.getSuccessResult();
   }
   
   /**
    * 通过id获取
    * @param id 主键id
    */
   @RequestMapping(value = "/getIeMaintenanceById/get.mvc")
   @ResponseBody
   public StandardResult getIeMaintenanceById(String pkId) {
	   IeMaintenance IeMaintenance = iIeMaintenanceService.getIeMaintenanceById(pkId);
       return StandardResult.getSuccessResult(IeMaintenance);
   }
   
   /**
    * 通过id批量删除
    * @param id 主键id
    * @return 删除结果集
    */
   @RequestMapping(value = "/delIeMaintenanceById/del.mvc")
   @ResponseBody
   public StandardResult delStopById(String pkIds) {
	   iIeMaintenanceService.delIeMaintenanceById(pkIds);
       return StandardResult.getSuccessResult();
   }
}

